import Vue from 'vue'
import Vuex from 'vuex'
import * as types from './types'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    key: '', // 登录验证的key
    user: {},
    token: null,
    title: '',
    A_URL:'http://v1-a.mingyukehui.com',
    B_URL:"http://v1-b.mingyukehui.com",
  },
  mutations: {
    // 设置登录验证的key
    setKey(state, key) {
      state.key = key
    },
    setToken(state, token) {
      state.token = token
    },
    delToken(state, token) {
      state.token = ''
    },
    setUser(state, user) {
      state.user = user
    },
    delUser(state, user) {
      state.user = {}
    },
  },
  actions: {
  },
  getters: {
    getKey: state => state.key,
    getToken: state => state.token,
    getUser: state => state.user,
  },
  modules: {
    //登录成功将, token保存在localStorage中
    [types.LOGIN]: (state, data) => {
      localStorage.token = data;
      state.token = data;
    },
    //退出登录将, token清空
    [types.LOGOUT]: (state) => {
      localStorage.removeItem('token');
      state.token = null
    }
  }

})
